package com.rwx.myjetpack

import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.rwx.myjetpack.UserModel.Companion.STATE_FAILED
import com.rwx.myjetpack.UserModel.Companion.STATE_LOADING
import com.rwx.myjetpack.UserModel.Companion.STATE_SUCCESS
import java.util.*

/**
 * Created by LH on 2023-05-25.
 * Time: 2023-05-25  14:17
 */
class LoginController {

    val userModel by lazy {
        UserModel()
    }

    fun checkUserNameState(account:String,callback:OnCheckUserNameStateResultCallback){
        userModel.checkUserState(account){
            Log.i("LoginController", "checkUserNameState: ${it}")
            when(it){
                0->{
                    callback.onExit()
                }
                1->{
                    callback.onNotExit()
                }
            }
        }
    }


    interface OnCheckUserNameStateResultCallback{
        fun onNotExit()
        fun onExit()
    }

    fun doLogin(username:String,password:String,callBack: OnDologinStateChange){


        if(TextUtils.isEmpty(username)){
            callBack.onAccountFormatError()
            return
        }

        if(TextUtils.isEmpty(password)){
            callBack.onPasswordEmpty()
            return
        }


        userModel.doLogin(username,password){
            when(it){
                STATE_LOADING ->{
                    callBack.onLoading()
                }
                STATE_SUCCESS ->{
                    callBack.onLoginSuccess()
                }
                STATE_FAILED ->{
                    callBack.onLoginFailed()
                }
            }
        }

    }

    interface OnDologinStateChange{
        fun onAccountFormatError()
        fun onPasswordEmpty()
        fun onLoading()
        fun onLoginSuccess()
        fun onLoginFailed()
    }

}